const app = getApp()
const { processListImages } = require('../../utils/image')
const request = require('../../utils/request.js');
Page({
  data: {
    courseList: [],
    page: 1,
    size: 10,
  },

  onLoad: function() {
    this.loadCourseList()
  },

  // 加载课程列表
  loadCourseList: function() {
    return new Promise((resolve, reject) => {
      request.request({
        url: `${app.globalData.baseUrl}/api/agricultural-knowledge/residues`,
        method: 'GET',
        data: {
          page: this.data.page,
          size: this.data.size,
        },
        header: {
          'Authorization': `Bearer ${app.getGlobalToken()}`
        },
        success: (res) => {
          const { list, total } = res.data.data
          // 使用工具函数处理图片URL
          const processedList = processListImages(list)
          this.setData({
            courseList: [...this.data.courseList, ...processedList],
          })
          resolve()
        },
        fail: reject
      })
    })
  },
  // 跳转到课程详情
  goToDetail: function(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/school/detail/detail?id=${id}`
    })
  }
}) 